import { defineStore } from 'pinia'
import { RouteRecordRaw } from 'vue-router'
import { MenuData, RouteData, menuList } from '@/data/MenuData'

/**
 * 菜单状态数据
 */
export const menuStore = defineStore('menuStore', {
  state: () => {
    return {
      menuRouter: [] as RouteRecordRaw[],
      menuData: [] as MenuData[]
    }
  },
  actions: {
    setMenuRouter(list: RouteRecordRaw[]){
      this.menuRouter = list
    },
    async initMenuData(){
      this.menuData = getMenuDataTest()
    }
  },
  persist:{
    storage: sessionStorage,
    key: 'menuStore',
    paths: ['menuData']
  }
})

export const lastRouteStore = defineStore('lastRouteStore', {
  state: () => {
    return {
      lastRouteList: [] as RouteData[]
    }
  },
  actions: {
    setLastRouteData(data: RouteData){
      const findData = this.lastRouteList.find(i => i.path === data.path)
      if(findData){
        return
      }
      this.lastRouteList.push(data)
      if(this.lastRouteList.length > 10){
        this.lastRouteList.shift()
      }
    }
  },
  persist:{
    storage: localStorage,
    key: 'lastRouteStore'
  }
})

function getMenuDataTest():MenuData[] {
  return menuList
}